/*
 * Curses window resize sample
 *
 */


#include <ncurses.h>

#include <signal.h>
#include <stdlib.h>
#include <sys/ioctl.h>

void q_intf (int);
void resize_intf (int);
void build_intf (void);
void redraw_intf (void);

static WINDOW *top_menu_bar;
static WINDOW *center_view_window;
static WINDOW *bottom_write_entry;

void
q_intf (int sig)
{

  endwin ();
  exit (EXIT_SUCCESS);

  (void)sig;

}

void
resize_intf (int sig)
{

  struct winsize s;

  ioctl(fileno(stdout), TIOCGWINSZ, &s);

  if (s.ws_row > 10 && s.ws_col > 10)
   {
    resizeterm (s.ws_row, s.ws_col);
    redraw_intf ();
   }

  (void)sig;

}

void
redraw_intf (void)
{

	clear ();

  wresize (top_menu_bar, 1, stdscr->_maxx - 1);
  wbkgd (top_menu_bar, COLOR_PAIR (1));

  wresize (center_view_window, stdscr->_maxy - 4, stdscr->_maxx - 1);
  wbkgd (center_view_window, COLOR_PAIR (2));

  mvderwin (bottom_write_entry, stdscr->_maxy - 1, 1);
  wresize (bottom_write_entry, 1, stdscr->_maxx - 1);
  wbkgd (bottom_write_entry, COLOR_PAIR (2));

  refresh ();

}

void
build_intf (void)
{


  top_menu_bar = subwin (stdscr, 1, stdscr->_maxx - 1, 0, 1);
 
  wbkgd (top_menu_bar, COLOR_PAIR (1));
  wprintw (top_menu_bar, "topic");

  center_view_window = subwin (stdscr, stdscr->_maxy - 4,\
                                       stdscr->_maxx - 1, 2, 1);
  wbkgd (center_view_window, COLOR_PAIR (2));

  scrollok (center_view_window, TRUE);

  bottom_write_entry = subwin (stdscr, 1, stdscr->_maxx - 1,\
                                       stdscr->_maxy - 1, 1);
  wbkgd (bottom_write_entry, COLOR_PAIR (3));
  scrollok (bottom_write_entry, TRUE);

  refresh ();

}

int
main (void)
{

  signal (SIGTERM, q_intf);
  signal (SIGWINCH, resize_intf);

  initscr ();
  start_color ();

  init_pair (1, COLOR_BLACK, COLOR_WHITE);
  init_pair (2, COLOR_WHITE, COLOR_BLUE);
  init_pair (3, COLOR_WHITE, COLOR_GREEN);

  build_intf ();


  for (;;usleep (10))
   {}

  endwin ();

  return EXIT_SUCCESS;

}

